package com.wanli.agent.homepage;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.SkeletonScreen;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wanli.agent.R;
import com.wanli.agent.base.BaseActivity;
import com.wanli.agent.base.DataCallBack;
import com.wanli.agent.bean.MerchantDetailsListBean;
import com.wanli.agent.homepage.adapter.MerchantLookListAdapter;
import com.wanli.agent.homepage.model.HomePageModelImpl;
import com.wanli.agent.homepage.model.IHomePageModel;
import com.wanli.agent.utils.DateTimeUtil;
import com.wanli.agent.utils.ToastUtil;
import com.wanli.agent.widget.AppTitleBar;
import com.wanli.agent.widget.ViewLoading;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MerchantDetailsListActivity extends BaseActivity implements OnRefreshLoadMoreListener {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.view_cancel)
    View viewCancel;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.tv_search)
    TextView tvSearch;

    private IHomePageModel homePageModel;
    private List<MerchantDetailsListBean.DataBean.ItemListBean> beanList;
    private MerchantLookListAdapter listAdapter;
    private int page = 1;
    private SkeletonScreen skeletonScreen;
    private String type;
    private String keyword = "";
    private String cycleType;
    private String param;
    private String start_time = "";
    private String end_time = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchant_details_list);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        cycleType = getIntent().getStringExtra("cycleType");
        param = getIntent().getStringExtra("param");

        refreshLayout.setRefreshHeader(new ClassicsHeader(mActivity));//设置Header
        refreshLayout.setRefreshFooter(new ClassicsFooter(mActivity));//设置Footer
        refreshLayout.setOnRefreshLoadMoreListener(this);

        editSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    if (!TextUtils.isEmpty(keyword)) {
                        refreshData();
                    }
                    return true;
                }
                return false;
            }
        });
        editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                keyword = editSearch.getText().toString().trim();
                if (!TextUtils.isEmpty(keyword)) {
                    viewCancel.setVisibility(View.VISIBLE);
                } else {
                    viewCancel.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        if ("date".equals(cycleType)) {
            start_time = param.replace(".", "-");
            end_time = start_time;
        } else if ("month".equals(cycleType)) {
            start_time = DateTimeUtil.getFirstDayOfMonth(param.replace(".", "-"));
            end_time = DateTimeUtil.getLastDayOfMonth(param.replace(".", "-"));
        }

    }

    private void initData() {
        type = getIntent().getStringExtra("type");
        if ("all_store_count".equals(type)) {
            titleBar.setTitle("总商户");
            if (!TextUtils.isEmpty(cycleType)) {
                titleBar.setTitle("商户");
            }
        } else if ("new_store_count".equals(type)) {
            titleBar.setTitle("新开商户");
        } else if ("high_quality_store_count".equals(type)) {
            titleBar.setTitle("优质商户");
        } else if ("normal_quality_store_count".equals(type)) {
            titleBar.setTitle("普通商户");
        } else if ("silent_store_count".equals(type)) {
            titleBar.setTitle("沉默商户");
        } else if ("checking_store_count".equals(type)) {
            titleBar.setTitle("审核中商户");
        } else if ("un_pass_store_count".equals(type)) {
            titleBar.setTitle("审核未通过商户");
        } else if ("waiting_for_income_profile_count".equals(type)) {
            titleBar.setTitle("待提交资料商户");
        }
        homePageModel = new HomePageModelImpl();
        beanList = new ArrayList<>();
        listAdapter = new MerchantLookListAdapter(beanList, cycleType);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);
        skeletonScreen = Skeleton.bind(recyclerView)
                .adapter(listAdapter)
                .color(R.color.shimmer_color_1)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1000)
                .count(20)
                .load(R.layout.item_skeleton_active_list)
                .show();
        requestData();
    }

    public void requestData() {
        homePageModel.requestGetStoreStatisticsEach(page, type, keyword, start_time, end_time, new DataCallBack<MerchantDetailsListBean>() {
            @Override
            public void onSuccessful(MerchantDetailsListBean bean) {
                if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                    beanList.addAll(bean.getData().getItemList());
                    if (page == 1) {
                        refreshLayout.finishRefresh();
                    } else {
                        refreshLayout.finishLoadMore();
                    }
                } else {
                    if (page != 1) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.finishRefresh();
                    }
                }
                if (beanList.size() > 0) {
                    llNoData.setVisibility(View.GONE);
                } else {
                    llNoData.setVisibility(View.VISIBLE);
                }
                listAdapter.notifyDataSetChanged();
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }

            @Override
            public void onFailed(String msg, String code) {
                listAdapter.notifyDataSetChanged();
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        requestData();
        refreshLayout.finishLoadMore(30000);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshData();
    }

    public void refreshData() {
        page = 1;
        beanList.clear();
        requestData();
        refreshLayout.finishRefresh(30000);
    }

    @OnClick({R.id.view_cancel, R.id.tv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.view_cancel:
                editSearch.setText("");
                keyword = "";
                refreshData();
                break;
            case R.id.tv_search:
                if (TextUtils.isEmpty(keyword)) {
                    ToastUtil.showShort("请输入搜索内容");
                    return;
                }
                ViewLoading.showProgress(mActivity, "加载中......");
                refreshData();
                break;
        }
    }
}
